global class GetSalerAccountRelationBatch implements Database.Batchable<Account> {
   

    global Iterable<Account> start(Database.batchableContext info){
       return [select Id,OwnerId ,Owner.SubCompany__c , Owner.FXProductLine__c,
                    (select Id ,UserId,User.subcompany__c,User.FXProductLine__c  from AccountTeamMembers   )
                    from Account where Owner.Profile.Name != '系统管理员' and  Owner.Profile.Name != 'System Administrator'];
    }

   
    global void execute(Database.BatchableContext BC, list<Account> accList ) {
/*
        //助理
        Map<String,String> assistantMap = new Map<String,String>();
        for(String s : Label.NoteAssitant_ReturnOrder.split(',') ){
            String[] ss =s.split('-'); 
            if(ss.size() > 0 )assistantMap.put(ss[0], ss[1]);
        }


        Integer year = System.now().year();
        Integer quarter = Integer.valueOf((System.now().month()-1) / 3 +1 );
        //检索已经存在的 主表       
        Map<String ,RightsConfirmation__c > rcMap = new Map<String,RightsConfirmation__c>();
        for(RightsConfirmation__c rc :  [select Id ,Company__c ,Year__c , Quarter__c from RightsConfirmation__c  where Year__c =:String.valueOf(year)+'年' and Quarter__c =: String.valueOf(quarter) ]){

            rcMap.put(rc.Company__c, rc);
        }
        //RightsConfirmation__c
        Set<Id> accSet = new Set<Id>();
        Set<Id> userSet  = new Set<Id>();

        for(Account acc : accList){
            accSet.add(acc.Id );
            userSet.add(acc.OwnerId );
            for(AccountTeamMember atm : acc.AccountTeamMembers ){
                userSet.add(atm.UserId );
            }

        }
        //检索已经存在的 子表      
         Map<String ,RightsConfirmationList__c> rclistMap = new Map<String,RightsConfirmationList__c>();
        for(RightsConfirmationList__c rcl : [select Id , Account__c ,User__c from RightsConfirmationList__c 
            where RightsConfirmation__r.Year__c =:String.valueOf(year)+'年' and RightsConfirmation__r.Quarter__c =: String.valueOf(quarter)
            and Account__c in:accSet and User__c in : userset  ]){

            rclistMap.put(rcl.Account__c+ ' '+rcl.User__c , rcl );

        }

        Map<String ,Map<String,RightsConfirmationList__c> > rclMap = new Map<String ,Map<String,RightsConfirmationList__c> >();
        for(Account acc : accList){

            //处理主表
            if(!rcMap.containsKey(acc.Owner.SubCompany__c)){
                if(assistantMap.get(acc.Owner.SubCompany__c ) == null ) System.debug('GetSalerAccountRelationBatch==>'+ acc.Owner.SubCompany__c);
                rcMap.put(acc.Owner.SubCompany__c, new RightsConfirmation__c(Year__c =String.valueOf(year)+'年', Quarter__c = String.valueOf(quarter) , Company__c =acc.Owner.SubCompany__c ,OwnerId = assistantMap.get(acc.Owner.SubCompany__c )));
            }
            

            //处理子表
            Map<String,RightsConfirmationList__c> tmp = rclMap.containsKey(acc.Owner.SubCompany__c) ? rclMap.get(acc.Owner.SubCompany__c): new Map<String,RightsConfirmationList__c>() ;

            RightsConfirmationList__c rcl =  tmp.containsKey(acc.Id + ' '+acc.OwnerId ) ? tmp.get (acc.Id + ' '+acc.OwnerId ):  rclistMap.containsKey(acc.Id + ' '+acc.OwnerId) ? rclistMap.get(acc.Id + ' '+acc.OwnerId) : new RightsConfirmationList__c();
            rcl.Account__c = acc.Id ; 
            rcl.User__c = acc.OwnerId;
            rcl.RightsType__c = '0';
            rcl.PruductLine__c = acc.Owner.FXProductLine__c;


            tmp.put(acc.Id + ' '+acc.OwnerId , rcl );

            rclMap.put(acc.Owner.SubCompany__c, tmp );

            for(AccountTeamMember atm : acc.AccountTeamMembers ){

                 //处理主表
                if(!rcMap.containsKey(atm.User.SubCompany__c))rcMap.put(atm.User.SubCompany__c, new RightsConfirmation__c(Year__c =String.valueOf(year)+'年', Quarter__c = String.valueOf(quarter) , Company__c =atm.User.SubCompany__c  ,OwnerId = assistantMap.get(atm.User.SubCompany__c  )));
                //处理子表
                 Map<String,RightsConfirmationList__c> tmp1 = rclMap.containsKey(atm.User.SubCompany__c) ? rclMap.get(atm.User.SubCompany__c): new Map<String,RightsConfirmationList__c>() ;

                RightsConfirmationList__c rcl1 =  tmp1.containsKey(acc.Id + ' '+atm.UserId  ) ? tmp.get (acc.Id + ' '+atm.UserId  ) :  rclistMap.containsKey(acc.Id + ' '+atm.UserId  ) ? rclistMap.get(acc.Id + ' '+atm.UserId  ) : new RightsConfirmationList__c();
                rcl1.Account__c = acc.Id ; 
                rcl1.User__c = atm.UserId ;
                rcl1.RightsType__c = '1';
                rcl1.PruductLine__c = atm.User.FXProductLine__c;

                tmp1.put(acc.Id + ' '+atm.UserId , rcl1 );
                rclMap.put(atm.User.SubCompany__c, tmp1 );

            }

        }

        //保存数据 
         System.debug('GetSalerAccountRelationBatch==>'+ rcMap.values());
        upsert rcMap.values();
        List<RightsConfirmationList__c> tList = new List<RightsConfirmationList__c>();
        for(String  company  : rcMap.keySet() ){

           Map<String ,RightsConfirmationList__c > tmp =  rclMap.get(company);
           if(tmp == null ) continue;
           for(RightsConfirmationList__c rl : tmp.values()){
                if(rl.RightsConfirmation__c == null ) rl.RightsConfirmation__c = rcMap.get(company).Id ; 
                tList.add(rl);
           }
        }
        if(tList.size() > 0 )upsert tList;
        */

        
    }

    global void finish(Database.BatchableContext BC) {
        /* Integer year = System.now().year();
        Integer quarter = Integer.valueOf((System.now().month()-1) / 3 +1 );

        String only = 'GetSalerAccountRelationSechduler'+year + quarter;
        List<SchedulerExcuetLog__c> seList =  [select Id , Only__c, Message__c , IsFinished__c , Stage1__c,Stage2__c,Stage3__c,Stage4__c from SchedulerExcuetLog__c  where Only__c =:only ];
        SchedulerExcuetLog__c se = seList.size() >0 ? seList[0] : new SchedulerExcuetLog__c(Only__c = only);
        se.IsFinished__c = true;

        upsert se;
        */

        

    }
}